package com.example.communitypro.service.impl;
import com.example.communitypro.mapper.*;
import com.example.communitypro.pojo.*;
import com.example.communitypro.service.CommentService;
import com.example.communitypro.util.LikeAndCollecionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import com.example.communitypro.util.result.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired(required=false)
    private CommentMapper commentMapper;

    @Autowired(required=false)
    private PostMapper postMapper;

    @Autowired(required=false)
    private UserCollectionMapper userCollectionMapper;

    @Autowired(required = false)
    private UserLikesMapper userLikesMapper;

    @Autowired(required = false)
    private UserReportMapper userReportMapper;

    @Autowired(required = false)
    private UserDetailsMapper userDetailsMapper;

    @Autowired(required = false)
    private UserInformationMapper userInformationMapper;



    @Override
    public Result addComment(Comment record) {
        try {
            record.setCommentStatus(2);
            record.setCommentCollectionNumber(0);
            record.setCommentLikeNumber(0);
            if(commentMapper.insertSelective(record)>0)
            {
                Post post = postMapper.selectByPrimaryKey(record.getPostNo());
                int now = post.getPostCommentNumber()+1;
                post.setPostCommentNumber(now);
                postMapper.updateByPrimaryKeySelective(post);
                List<Comment> hotComments = commentMapper.getHotComment(record.getPostNo());
                List<UserDetails> hotuserDetails = new ArrayList<>();
                List<UserInformation> hotuserInformations = new ArrayList<>();
                for (Comment hotComment : hotComments){
                    hotuserDetails.add(userDetailsMapper.selectByUser(hotComment.getUserNo()));
                    hotuserInformations.add(userInformationMapper.selectByPrimaryKey(hotComment.getUserNo()));
                }
                List<Integer> like = LikeAndCollecionUtil.like1(hotComments,record.getUserNo());
                List<Integer> collection = LikeAndCollecionUtil.collecion1(hotComments,record.getUserNo());
                return Result.ok().message("新增评论成功").data("comments",hotComments).data("user1",hotuserDetails).data("user1",hotuserInformations).data("like",like).data("likes",like.size()).data("collection",collection).data("collections",collection.size());
            }
            else{
                return Result.error().message("新增评论失败");
            }
        }catch (Exception e){
            return Result.error().message("新增评论失败");
        }
    }

    @Override
    public Result dropComment(int commentNo) {
        try {
            if(commentMapper.selectByPrimaryKey(commentNo)!=null)
            {
                Comment comment = commentMapper.selectByPrimaryKey(commentNo);
                System.out.println(comment);
                Post post = postMapper.selectByPrimaryKey(comment.getPostNo());
                System.out.println(post);
                int now = post.getPostCommentNumber()-1;
                post.setPostCommentNumber(now);
                postMapper.updateByPrimaryKeySelective(post);
                userCollectionMapper.deleteCommentCollection(commentNo);
                userLikesMapper.deleteCommentLike(commentNo);
                userReportMapper.deleteTypeReport(commentNo,2);
                commentMapper.deleteByPrimaryKey(commentNo);
                return Result.ok().message("删除评论成功");
            }
            else{
                System.out.println("失败");
                return Result.error().message("删除评论失败");
            }
        }catch (Exception e){
            return Result.error().message("删除评论失败");
        }
    }

    @Override
    public Result modifyComment(Comment record) {
        try{
            if (commentMapper.updateByPrimaryKeySelective(record)>0){
                return Result.ok().message("修改成功");
            }
            else return Result.error().message("修改失败：未找到对应记录");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryComment(int commentNo) {
        try {
            Comment comment=commentMapper.selectByPrimaryKey(commentNo);
            if(comment!=null)
            {
                return Result.ok().message("查找评论成功").data("comment",comment);
            }
            else{
                return Result.error().message("查找评论失败");
            }
        }catch (Exception e){
            return Result.error().message("查找评论失败");
        }
    }

    @Override
    public Result queryUserComment(int user_no) {
        try {
            List<Comment> comments = commentMapper.getUserComment(user_no);
            if (comments!=null){
                return Result.ok().data("comments",comments);
            }
            else {
                return Result.error().message("查找用户所有评论成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryUserPassComment(int user_no, int comment_status) {
        try {
            List<Comment> comments = commentMapper.getUserPassComment(user_no,comment_status);
            if (comments!=null){
                return Result.ok().data("comments",comments);
            }
            else {
                return Result.error().message("查找用户所有状态评论成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryAllPassComment(int comment_status) {
        try {
            List<Comment> comments = commentMapper.getAllPassComment(comment_status);
            if (comments!=null){
                return Result.ok().data("comments",comments);
            }
            else {
                return Result.error().message("查找用户所有评论成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryPostComment(int post_no, int user_no) {
        try {
            List<Comment> comments = commentMapper.getPostComment(post_no);
            if (comments!=null){
                List<UserDetails> userDetails = new ArrayList<>();
                List<UserInformation> userInformations = new ArrayList<>();
                for (Comment comment : comments){
                    userDetails.add(userDetailsMapper.selectByUser(comment.getUserNo()));
                    userInformations.add(userInformationMapper.selectByPrimaryKey(comment.getUserNo()));
                }
                List<Integer> like = LikeAndCollecionUtil.like1(comments,user_no);
                List<Integer> collection = LikeAndCollecionUtil.collecion1(comments,user_no);
                return Result.ok().data("comments",comments).data("user1",userDetails).data("user2",userInformations).data("like",like).data("likes",like.size()).data("collection",collection).data("collections",collection.size());
            }
            else {
                return Result.error().message("查找某帖子所有评论（顺序）成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryHotComment(int post_no, int user_no) {
        try {
            List<Comment> comments = commentMapper.getHotComment(post_no);
            if (comments!=null){
                List<UserDetails> userDetails = new ArrayList<>();
                List<UserInformation> userInformations = new ArrayList<>();
                comments.removeIf(comment -> comment.getReplyNo() != null);
                for (Comment comment : comments){
                    userDetails.add(userDetailsMapper.selectByUser(comment.getUserNo()));
                    userInformations.add(userInformationMapper.selectByPrimaryKey(comment.getUserNo()));
                }
                List<Integer> like = LikeAndCollecionUtil.like1(comments,user_no);
                List<Integer> collection = LikeAndCollecionUtil.collecion1(comments,user_no);
                return Result.ok().data("comments",comments).data("user1",userDetails).data("user2",userInformations).data("like",like).data("likes",like.size()).data("collection",collection).data("collections",collection.size());
            }
            else {
                return Result.error().message("查找某帖子所有评论（热度）成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryAllComment() {
        try {
            List<Comment> comments = commentMapper.getAllComment();
            if (comments!=null){
                List<UserDetails> userDetails = new ArrayList<>();
                List<UserInformation> userInformations = new ArrayList<>();
                for (Comment comment : comments){
                    userDetails.add(userDetailsMapper.selectByUser(comment.getUserNo()));
                    userInformations.add(userInformationMapper.selectByPrimaryKey(comment.getUserNo()));
                }

                return Result.ok().data("comments",comments).count(comments.size()).data("user1",userDetails).data("user2",userInformations);
            }
            else {
                return Result.error().message("查找所有评论成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result dropPostComment(int post_no) {
        try {
            List<Comment> comments = commentMapper.getPostComment(post_no);
            int i;
            for(i=0;i<comments.size();i++)
            {
                userCollectionMapper.deleteCommentCollection(comments.get(i).getCommentNo());
                userLikesMapper.deleteCommentLike(comments.get(i).getCommentNo());
                userReportMapper.deleteTypeReport(comments.get(i).getCommentNo(),2);
                commentMapper.deleteByPrimaryKey(comments.get(i).getCommentNo());
            }
            if (comments!=null){
                return Result.ok().data("comments",comments).count(comments.size());
            }
            else {
                return Result.error().message("查找所有评论成功");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryReply(Integer commentNo,Integer userNo) {
        try {
            List<Comment> comments = commentMapper.getReply(commentNo);
            if (comments!=null){
                List<UserDetails> userDetails = new ArrayList<>();
                List<UserInformation> userInformations = new ArrayList<>();
                for (Comment comment : comments){
                    userDetails.add(userDetailsMapper.selectByUser(comment.getUserNo()));
                    userInformations.add(userInformationMapper.selectByPrimaryKey(comment.getUserNo()));
                }
                List<Integer> like = LikeAndCollecionUtil.like1(comments,userNo);
                List<Integer> collection = LikeAndCollecionUtil.collecion1(comments,userNo);
                return Result.ok().data("comments",comments).data("user1",userDetails).data("user2",userInformations).data("like",like).data("likes",like.size()).data("collection",collection).data("collections",collection.size());
            }

            else {
                return Result.error().message("查找所有评论失败");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result queryReplyUser(Integer commentNo) {
        try {
            Comment comment=commentMapper.selectByPrimaryKey(commentNo);
            UserInformation userInformation=userInformationMapper.selectByPrimaryKey(comment.getUserNo());
            if(userInformation!=null)
            {
                return Result.ok().message("查找回复用户成功").data("userInformation",userInformation);
            }
            else{
                return Result.error().message("查找回复用户失败");
            }
        }catch (Exception e){
            return Result.error().message("查找回复用户失败");
        }
    }

    @Override
    public Result queryFather(Integer commentNo,Integer userNo) {
        try {
            List<Comment> comments = commentMapper.getFather(commentNo);
            System.out.println(comments);
            if (comments!=null){
                List<UserDetails> userDetails = new ArrayList<>();
                List<UserInformation> userInformations = new ArrayList<>();
                for (Comment comment : comments){
                    userDetails.add(userDetailsMapper.selectByUser(comment.getUserNo()));
                    userInformations.add(userInformationMapper.selectByPrimaryKey(comment.getUserNo()));
                }
                List<Integer> like = LikeAndCollecionUtil.like1(comments,userNo);
                List<Integer> collection = LikeAndCollecionUtil.collecion1(comments,userNo);
                System.out.println(userDetails);
                System.out.println(userInformations);
                return Result.ok().data("comments",comments).data("user1",userDetails).data("user2",userInformations).data("like",like).data("likes",like.size()).data("collection",collection).data("collections",collection.size());
            }

            else {
                return Result.error().message("查找所有评论失败");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }
}
